package com.dingshi.security.view;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingshi.security.R;

public class MainTitleView extends FrameLayout {

	public TextView clock_am_panel, clock_time_panel, attendant, residential;
	public BatteryView batteryView;
	public SignalView signalView;
	private LayoutInflater mInflater;
	private LinearLayout mBarView;
	public static boolean isRungingtime = false;;
	public Timer timetask;
	Handler cwjHandler = new Handler();

	private static MainTitleView instance;

	public static MainTitleView getInstance(Context context) {
		if (null == instance) {
			instance = new MainTitleView(context);
		}
		return instance;
	}

	public MainTitleView(Context context) {
		super(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBarView = (LinearLayout) mInflater.inflate(R.layout.maintitle, null);
		addView(mBarView);

		clock_am_panel = (TextView) mBarView.findViewById(R.id.clock_am_panel);
		clock_time_panel = (TextView) mBarView
				.findViewById(R.id.clock_time_panel);

		attendant = (TextView) mBarView.findViewById(R.id.attendant);
		residential = (TextView) mBarView.findViewById(R.id.residential);
		batteryView = (BatteryView) mBarView.findViewById(R.id.batterys);
		signalView = (SignalView) mBarView.findViewById(R.id.signals);
		instance = this;
	}

	public MainTitleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBarView = (LinearLayout) mInflater.inflate(R.layout.maintitle, null);
		addView(mBarView);

		clock_am_panel = (TextView) mBarView.findViewById(R.id.clock_am_panel);
		clock_time_panel = (TextView) mBarView
				.findViewById(R.id.clock_time_panel);
		attendant = (TextView) mBarView.findViewById(R.id.attendant);
		residential = (TextView) mBarView.findViewById(R.id.residential);
		batteryView = (BatteryView) mBarView.findViewById(R.id.batterys);
		signalView = (SignalView) mBarView.findViewById(R.id.signals);

		instance = this;
	}

	public void setBaatery(int value) {
		Log.e("电量", value + " ");
		batteryView.setBaateryValue(value);
	}

	public void setSignal(int value) {
		signalView.setSignalValue(value);
	}

	public void setTime(String time) {
		clock_time_panel.setText(time);
	}

	public void setAmOrPm(String amorpm) {
		clock_am_panel.setText(amorpm);
	}

	public void setesidential(String residentials) {
		residential.setText(residentials);
	}

	public void setAttendant(String attendants) {
		attendant.setText(attendants);
	}

	static int x = 0;

	public void timeStop() {
		if (timetask != null) {
			timetask.cancel();
			timetask = null;
		}
	}

	public void timeStart() {

		timetask = new Timer();
		timetask.schedule(new TimerTask() {

			@Override
			public void run() {

				Calendar ca = Calendar.getInstance();
				final int year = ca.get(Calendar.YEAR);// 获取年份
				final int month = ca.get(Calendar.MONTH);// 获取月份
				final int day = ca.get(Calendar.DATE);// 获取日
				final int minute = ca.get(Calendar.MINUTE);// 分
				final int hour = ca.get(Calendar.HOUR);// 小时
				final int second = ca.get(Calendar.SECOND);// 秒
				final int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
				final int amOrPm = ca.get(Calendar.AM_PM);

				cwjHandler.post(new Runnable() {

					@Override
					public void run() {

						clock_am_panel.setText(amOrPm == 0 ? "AM" : "PM");

						clock_time_panel.setText((hour < 10 ? " 0" + hour : " "
								+ hour)
								+ ":"
								+ (minute < 10 ? " 0" + minute : " " + minute
										+ " "));

					}
				});

			}
		}, 1000, 1000);

	}
}
